﻿/**
 * @file deque_pushpop.c
 * Copyright (c) 2021 Gaaagaa. All rights reserved.
 * 
 * @author  : Gaaagaa
 * @date    : 2021-06-01
 * @version : 1.0.0.0
 * @brief   : 使用 双端队列 容器进行数据的 压入/弹出 操作。
 */

/**
 * The MIT License (MIT)
 * Copyright (c) Gaaagaa. All rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "std_deque.h"
#include "std_mdebug.h"
#include <stdio.h>

////////////////////////////////////////////////////////////////////////////////

/**********************************************************/
/**
 * @brief 打印输出 xstd_deque_t 对象的各个节点元素。
 */
xstd_void_t printf_deque_int32(xdeq_this_t xdeq_this)
{
    xstd_size_t xst_count = 0;
    xdeq_iter_t xdeq_iter = std_deque_begin(xdeq_this);
    xdeq_iter_t xdeq_vend = std_deque_end(xdeq_this);

    if (std_deque_size(xdeq_this) <= 0)
    {
        printf("deque(0) \n");
        return;
    }

    printf("deque(%d) [[[\n", (xstd_int32_t)std_deque_size(xdeq_this));

    for (xst_count  = 0;
         xdeq_iter != xdeq_vend;
         xdeq_iter  = std_deque_next(xdeq_this, xdeq_iter))
    {
        printf("[%3d]=(%3d)",
            (xstd_int32_t)xst_count, std_deque_elem_int32(xdeq_this, xdeq_iter));

        if (++xst_count < std_deque_size(xdeq_this))
        {
            if (0 == (xst_count & 7))
                printf("\n");
            else
                printf(" , ");
        }
        else
        {
            printf("\n]]] deque\n\n");
        }
    }
}

////////////////////////////////////////////////////////////////////////////////

int main(int argc, char * argv[])
{
    xmdb_this_t xmdb_comm = XSTD_NULL;
    xstd_int32_t  xit_value = 0;
    xstd_int32_t  xit_count = 256;
    xdeq_this_t xdeq_this = XSTD_NULL;

    //======================================

    xmdb_comm = (xmdb_this_t)std_mdbcomm_start();

    printf("%d. mem count : %d\n", __LINE__, (xstd_int32_t)std_mdbcomm_count());

    xdeq_this = std_deque_create_int32();

    printf("%d. mem count : %d\n", __LINE__, (xstd_int32_t)std_mdbcomm_count());

    //======================================
    // push

    printf("PUSH: ");

    for (xit_value = 1; xit_value <= xit_count; ++xit_value)
    {
        std_deque_push_front_int32(xdeq_this, (xit_count + 1) - xit_value);
        std_deque_push_back_int32(xdeq_this, (xit_count + 1) - xit_value);
    }

    printf_deque_int32(xdeq_this);

    printf("%d. mem count : %d\n", __LINE__, (xstd_int32_t)std_mdbcomm_count());

    //======================================
    // pop

    printf("POP 1: ");

    for (xit_value = 1; xit_value <= (xit_count / 2); ++xit_value)
    {
        std_deque_pop_back(xdeq_this);
    }

    printf_deque_int32(xdeq_this);

    printf("%d. mem count : %d\n", __LINE__, (xstd_int32_t)std_mdbcomm_count());

    //======================================
    // pop

    printf("POP 2: ");

    for (xit_value = 1; xit_value <= (xit_count / 2); ++xit_value)
    {
        std_deque_pop_front(xdeq_this);
    }

    printf_deque_int32(xdeq_this);

    printf("%d. mem count : %d\n", __LINE__, (xstd_int32_t)std_mdbcomm_count());

    //======================================
    // pop

    printf("POP 3: ");

    for (xit_value = 1; xit_value <= (xit_count / 2); ++xit_value)
    {
        std_deque_pop_back(xdeq_this);
    }

    printf_deque_int32(xdeq_this);

    printf("%d. mem count : %d\n", __LINE__, (xstd_int32_t)std_mdbcomm_count());

    //======================================
    // pop

    printf("POP 4: ");

    for (xit_value = 1; xit_value <= (xit_count / 2); ++xit_value)
    {
        std_deque_pop_front(xdeq_this);
    }

    printf_deque_int32(xdeq_this);

    printf("%d. mem count : %d\n", __LINE__, (xstd_int32_t)std_mdbcomm_count());

    //======================================
    // shrink

    printf("SHRINK : ");
    std_deque_shrink(xdeq_this);
    printf_deque_int32(xdeq_this);

    printf("%d. mem count : %d\n", __LINE__, (xstd_int32_t)std_mdbcomm_count());

    //======================================

    std_deque_destroy(xdeq_this);

    printf("%d. mem count : %d\n", __LINE__, (xstd_int32_t)std_mdbcomm_count());

    std_mdbcomm_stop();

    //======================================

    return 0;
}


